﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace FlexExplorer
{
	public delegate void AdvanceDelegate(int step);
	public delegate void SetLabelDelegate(string textLabel);
	public delegate void CloseDelegate();

	public partial class CopyProgressForm : Form
	{
		private bool userClose = true;
		private Thread copyThread;

		public CopyProgressForm(Thread thread)
		{
			InitializeComponent();
			copyThread = thread;
		}

		public void SetFileLength(int size)
		{
			if (this.InvokeRequired)
			{
				this.Invoke(new AdvanceDelegate(SetFileLength),
							new object[] { size });
				return;
			}

			copyProgressBar.Minimum = 0;	
			copyProgressBar.Maximum = size;
			copyProgressBar.Value = 0;
		}

		public Label cpyFileNameLabel;

		public void Advance(int step)
		{

			if (this.InvokeRequired)
			{
				this.Invoke(new AdvanceDelegate(Advance),
							new object[] { step });
				return;
			}

			copyProgressBar.Step = step;
			copyProgressBar.PerformStep();
		}

		public void SetLabel(string textLabel)
		{
			if (this.InvokeRequired)
			{
				this.Invoke(new SetLabelDelegate(SetLabel),
							new object[] { textLabel });
				return;
			}

			CopyFileNameLabel.Text = textLabel;
		}

		public void SetFullCopyfileSizeLabel(string textLabel)
		{
			if (this.InvokeRequired)
			{
				this.Invoke(new SetLabelDelegate(SetFullCopyfileSizeLabel),
							new object[] { textLabel });
				return;
			}

			FullCopyfileSizeLabel.Text = textLabel;
		}

		public void SetCopiedFileSizeLabel(string textLabel)
		{
			if (this.InvokeRequired)
			{
				this.Invoke(new SetLabelDelegate(SetCopiedFileSizeLabel),
							new object[] { textLabel });
				return;
			}

			CopiedFileSizeLabel.Text = textLabel;
		}

		public void SetCopyPercentsCompleteLabel(string textLabel)
		{
			if (this.InvokeRequired)
			{
				this.Invoke(new SetLabelDelegate(SetCopyPercentsCompleteLabel),
							new object[] { textLabel });
				return;
			}

			CopyPercentsCompleteLabel.Text = textLabel;
		}

		public void DoClose()
		{
			if (this.InvokeRequired)
			{
				this.Invoke(new CloseDelegate(DoClose));
				return;
			}

			userClose = false;
			Close();
		}

		private void CancelBtn_Click(object sender, EventArgs e)
		{
			Close();
		}

		private void CopyProgressForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (userClose)
			{
				copyThread.Abort();
			}
		}		
	}
}